<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<link rel="stylesheet" href="${webcontext}/res/style/S.css" />
	<script type="text/javascript" src="${webcontext}/res/js/jquery.min.js"></script>
	<script type="text/javascript" src="${webcontext}/res/js/S.js"></script>
	<script type="text/javascript">
		
		$(function(){
			var tbl  =new S.Table({
				el:'#tbl01',
				pager:true,
				pageSize:20,
				renders:{
					title:function(title,d){
						if(d.draft){return '<span style="color:#006dcc;">[草稿]</span>'+title;}
						return title;
					}
				},
				dbclick:function(data){
					if(data){
						editBlog(data.id);
					}
				},
				menus:{
					'修改':function(tr){
						var idx = $(tr).index();
						var data = tbl.data[idx];
						if(data==null){return;}
						editBlog(data.id);
					},
					'预览':function(tr){
						var idx = $(tr).index();
						var data = tbl.data[idx];
						if(data==null){return;}
						window.open('${webcontext }/${pathvar.admin}/blog/article/preview?id='+data.id);
					},
					'删除':function(tr){
						var idx = $(tr).index();
						var data = tbl.data[idx];
						if(data==null){return;}
						deleteBlog([data]);
					}
				}
			});
			
			var tree = new S.Tree({
				el:'#catelogtree',
				ds:'${webcontext }/${pathvar.admin}/blog/catelog/list',
				render:function(data){
					return '<i class="icons book_spelling"></i> '+data.name;
				},
				click:function(node){
					if(node){
						$('#catelog').val(node.data.id);
					}else{
						$('#catelog').val('');
					}
					tbl.reload();
				}
			});

			$('#btn_tree_catelog_empty').click(function(){
				tree.clear();
				$('#catelog').val('');
				tbl.reload();
			});
			
			$('#btn_catelog_add').click(function(){
				
				var dlg = new S.Dialog({
					title:'增加分类',
					width:400,
					height:200,
					url:'${webcontext }/${pathvar.admin}/blog/catelog/edit',
					buttons:{
						'保存':function(){
							if(!this.form.validate()){new S.Info({text:'填写不完整',type:'warn'}).show();return;}
							S.request({
								url:'${webcontext }/${pathvar.admin}/blog/catelog/save',
								data:this.form.param(),
								success:function(){
									new S.Info({text:'操作成功',type:'success'}).show();
									tree.reload();
									dlg.remove();
								}
							});
						},
						'关闭':function(){
							this.remove();
						}
					}
				});
				dlg.show();
				
			});
			
			$('#btn_catelog_edit').click(function(){
				
				var items = tree.getSelectedItems();
				if(items.length !=1){new S.Info({text:'请选择分类',type:'warn'}).show();return;}
				
				var dlg = new S.Dialog({
					title:'修改分类',
					width:400,
					height:200,
					url:'${webcontext }/${pathvar.admin}/blog/catelog/edit?id='+items[0].data.id,
					buttons:{
						'保存':function(){
							if(!this.form.validate()){new S.Info({text:'填写不完整',type:'warn'}).show();return;}
							S.request({
								url:'${webcontext }/${pathvar.admin}/blog/catelog/update',
								data:this.form.param(),
								success:function(){
									new S.Info({text:'操作成功',type:'success'}).show();
									tree.reload();
									dlg.remove();
								}
							});
						},
						'关闭':function(){
							this.remove();
						}
					}
				});
				dlg.show();
			});
			$('#btn_catelog_delete').click(function(){
				var items = tree.getSelectedItems();
				if(items.length ==0){new S.Info({text:'请选择分类',type:'warn'}).show();return;}
				S.confirm('警告','确认要删除该分类?',function(){
					S.request({
						url:'${webcontext }/${pathvar.admin}/blog/catelog/delete',
						data:{id:items[0].data.id},
						success:function(){
							new S.Info({text:'操作成功',type:'success'}).show();
							tree.reload();
						}
					});
				},'danger');
			});
			
			$('#btn_blog_add').click(function(){
				window.location.href='${webcontext }/${pathvar.admin}/blog/article/edit';
			});
			
			function editBlog(id){
				window.location.href='${webcontext }/${pathvar.admin}/blog/article/edit?id='+id;
			}
			$('#btn_blog_edit').click(function(){
				var items = tbl.getCheckedRow();
				if(items.length !=1){new S.Info({text:'请选择1行信息',type:'warn'}).show();return;}
				editBlog(items[0].id);
			});
			
			
			function deleteBlog(items){
				S.confirm('警告','确订要删除选中的博客?',function(){
					var ids = [];
					S.each(items,function(i,item){
						ids.push(item.id);						
					});
					S.request({
						url:'${webcontext }/${pathvar.admin}/blog/article/delete',
						data:{ids:ids.join(',')},
						success:function(){
							new S.Info({text:'操作成功',type:'warn'}).show();
							tbl.reload();
							tree.reload();
						}
					});
				},'warn');
			}
			
			$('#btn_blog_delete').click(function(){
				var items = tbl.getCheckedRow();
				if(items.length == 0){new S.Info({text:'请选择信息',type:'warn'}).show();return;}
				
				deleteBlog(items);
			});
		});	
	</script>
</head>
<body>
	<div class="filled-box">
		<div class="sidebar" style="width:300px;">
			<div class="toolbar">
				<span class="btn" id="btn_catelog_add"><i class="icons book_add"></i> 增加</span>
				<span class="btn" id="btn_catelog_edit"><i class="icons book_edit"></i> 修改</span>
				<span class="btn" id="btn_catelog_delete"><i class="icons book_delete"></i> 删除</span>
			</div>
			<div class="content" id="catelogtree">
				<div id="btn_tree_catelog_empty" style="cursor: pointer;"> <i class="icons book_spelling"></i> 文章分类</div>
			</div>
		</div>
		<div class="rightside" style="margin-left: 300px;">
			<div class="toolbar">
				<span class="btn" id="btn_blog_add"><i class="icons page_add"></i> 增加</span>
				<span class="btn" id="btn_blog_edit"><i class="icons page_edit"></i> 修改</span>
				<span class="btn" id="btn_blog_delete"><i class="icons page_delete"></i> 删除</span>
			</div>
			<div class="content">
				<form name="frm001">
					<input type="hidden" name="catelog" id="catelog">
				</form>
				<table id="tbl01" form="frm001" height="600" ds="${webcontext }/${pathvar.admin}/blog/article/list">
					<tr>
						<td width="30" seq></td>
						<td width="50" checkall></td>
						<td>标题</td>
						<td width="100">作者</td>
						<td width="100">目录</td>
						<td width="150">标签</td>
						<td width="100">评论</td>
						<td width="150">创建日期</td>
						<td width="150">发表日期</td>
					</tr>
					<tr>
						<td>@{seq}</td>
						<td><input type="checkbox" name="id" value="@{id}" /></td>
						<td>@{title}</td>
						<td>@{creatorName}</td>
						<td>@{catelogName}</td>
						<td>@{tags}</td>
						<td>@{commentCount}</td>
						<td>@{createTime}</td>
						<td>@{publishTime}</td>
					</tr>
				</table>
			</div>
		</div>
	</div>
</body>
</html>